//
//  FundManagementRecordView.m
//  优学荟管理系统
//
//  Created by 星道 on 17/9/5.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "FundManagementRecordView.h"

@implementation FundManagementRecordView
{
    void (^_complete)(NSInteger);
    NSArray <NSString *>*_titleArr;

    FundManagementRecordButton *_tempButton;
}


+ (instancetype)ConfigWithTitleArr:(NSArray<NSString *> *)titleArr height:(CGFloat)height complete:(void (^)(NSInteger))complete {

    return [[self alloc] initWithTitleArr:titleArr height:height complete:complete];
}

- (instancetype)initWithTitleArr:(NSArray <NSString *>*)titleArr height:(CGFloat)height complete:(void (^)(NSInteger index))complete {

    self = [super init];
    if (self) {

        _titleArr = titleArr;
        _complete = complete;

        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);

        [self ConfigUI];
    }
    return self;
}




- (void)ConfigUI {

    [_titleArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        FundManagementRecordButton *button = [FundManagementRecordButton ConfigWithTitle:obj index:idx];
        [button addTarget:self action:@selector(Click:) forControlEvents:(UIControlEventTouchUpInside)];

        CGFloat x = idx * (SCREEN_WIDTH/_titleArr.count);
        CGFloat y = 0;
        CGFloat w = SCREEN_WIDTH/_titleArr.count;
        CGFloat h = self.frame.size.height;
        button.frame = CGRectMake(x, y, w, h);
        [self addSubview:button];

        button.selected = idx == 0 ? YES:NO;
        idx == 0 ? _tempButton = button:nil;
    }];
}

/** 点击事件 */
- (void)Click:(FundManagementRecordButton *)button {

    if (button == _tempButton) {
        return;
    }

    _tempButton.selected = !_tempButton.selected;
    _tempButton = button;
    _tempButton.selected = !_tempButton.selected;

    _complete ? _complete (button.tag):nil;
}
@end
